/**
 * Created by Administrator on 2017/1/7.
 */
const _path = require('path');
const fs = require('fs');

function Sample(name, path) {
    if (name) {
        this.name = name;
    }

    if (path) {
        this.path = _path.join(Sample.prototype.path, path);
    }
}

Sample.prototype = {
    name: '示例',
    path: 'D:\\Projects\\CSharp Projects'
};

Sample.prototype.getChapters = function () {
    var typeofarg1, typeofarg2, excludes, callback;

    if (arguments.length == 1) {
        typeofarg1 = typeof arguments[0];
        if (typeofarg1 != 'function') {
            throw new Error('第1参数类型必须是函数回调');
        }
        callback = arguments[0];
        fs.readdir(this.path, function (err, files) {
            if (err) throw err;
            callback(null, files);
        });
    }

    if (arguments.length == 2) {
        typeofarg1 = typeof arguments[0];
        if (Array.isArray(typeofarg1)) {
            throw new Error('第1参数类型必须数组');
        }
        excludes = arguments[0];

        typeofarg2 = typeof arguments[1];
        if (typeofarg2 != 'function') {
            throw new Error('第2参数类型必须是函数回调');
        }

        callback = arguments[1];
        fs.readdir(this.path, function (err, files) {
            if (err) throw err;
            var filter = [];
            files.forEach(function (item) {
                if (excludes.indexOf(item) == -1) {
                    filter.push(item);
                }
            });
            callback(null, filter);
        });
    }
};

Sample.prototype.toString = function () {
    return this.name;
};

Object.defineProperty(Sample.prototype, 'chapters', {
    get: function () {
        return fs.readdirSync(Sample.prototype.path);
    }
});

module.exports = Sample;